/*
 * Copyright (c) 2022 CompanyNameMagicTag.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup UI_Common
 * @{
 *
 * @brief Defines common UI capabilities, such as image and text processing.
 *
 */

/**
 * @file image_cache_manager.h
 *
 * @brief Declares the <b>ImageCacheManager</b> class of the graphics module, which provides functions for image load
 * and cache manager.
 *
 */

#ifndef IMAGE_CACHE_MANAGER_H
#define IMAGE_CACHE_MANAGER_H
#include <string>
#include <cstdint>
#include <unordered_map>
#include "gfx_utils/image_info.h"
#include "gfx_utils/heap_base.h"
#include "graphic_config.h"

#if ENABLE_IMAGE_PACKER
namespace OHOS {
/**
 * @brief Declares the <b>ImageCacheManager</b> class of the graphics module.
 *        This class provides functions for image load and cache manager.
 *
 */
class ImageCacheManager : public HeapBase {
public:
    /**
     * @brief Get <b>ImageCacheManager</b> instance.
     */
    static ImageCacheManager& GetInstance();
    /**
     * @brief load all at a time, (ps)ram was allocate once for all.
     */
    bool LoadAllInMultiRes(const std::string& file, int offset = 0);

    /**
     * @brief load one at a time, (ps)ram was allocate one for one.
     */
    ImageInfo* LoadOneInMultiRes(uint32_t resId, const std::string& file, int offset = 0);

    /**
     * @brief unload one for loading one at a time.
     */
    bool UnloadOneInMultiRes(uint32_t resId, const std::string& file);

    /**
     * @brief unload all for loading all or one at a time.
     */
    bool UnloadAllInMultiRes(const std::string& file);

    /**
     * @brief load single res file.
     */
    ImageInfo* LoadSingleRes(const std::string& file);

    /**
     * @brief unload single res file.
     */
    bool UnloadSingleRes(const std::string& file);

private:
    struct MultiImageInfo {
        bool loadAllOnce;
        uint32_t dirId;
        uint32_t count;
        ImageInfo** info;
        uint8_t* contentAddr;
        uint32_t contentSize;
        uint32_t loadedCount;
    };

    struct BinHeader {
        uint32_t dirId;
        uint32_t count;
    };
    ImageCacheManager() {};
    ~ImageCacheManager() {};
    bool InitMultiImgInfo(BinHeader& header, MultiImageInfo*& multiImgInfo, bool isLoadAllOnce);
    bool LoadAllImgInfo(FILE* fp, MultiImageInfo& multiImgInfo);
    void FreeInfo(MultiImageInfo& multiImgInfo);
    ImageInfo* LoadSingleImgInfo(FILE* fp, MultiImageInfo& multiImgInfo, uint32_t imgIndex);
    std::unordered_map<std::string, ImageInfo*> imageInfoForSingle_;
    std::unordered_map<std::string, MultiImageInfo*> imageInfoForMulti_;
};
} // OHOS
#endif // ENABLE_IMAGE_PACKER
#endif // IMAGE_CACHE_MANAGER_H
/**
 * @}
 */
